

import UIKit
import ObjectMapper
import SnapKit

class BAHomeViewController: UIViewController {
    
    private let siteListView = BASiteListView()
    private let cityView = BACityView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor("#E9ECF2")
        setupUI()
    }
    
    private func setupUI() {
        if UIDevice.isX() {
            UIApplication.shared.setStatusBarHidden(false, with: .none)
        }
        let statusBack = UIView()
        statusBack.backgroundColor = colorWhite
        statusBack.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 100)
        view.addSubview(statusBack)
        
        let navBar = UIView()
        navBar.backgroundColor = colorWhite
        view.addSubview(navBar)
        
        let menuButton = UIButton()
        menuButton.setImage(#imageLiteral(resourceName: "button_menu_mormal"), for: .normal)
        menuButton.setImage(#imageLiteral(resourceName: "button_menu_selected"), for: .selected)
        menuButton.addTarget(self, action: #selector(openMenuView), for: .touchUpInside)
        navBar.addSubview(menuButton)
        
        let searchButton = UIButton()
        searchButton.setImage(#imageLiteral(resourceName: "button_search_normal"), for: .normal)
        searchButton.setImage(#imageLiteral(resourceName: "button_search_normal"), for: .selected)
        searchButton.addTarget(self, action: #selector(openSearchView), for: .touchUpInside)
        navBar.addSubview(searchButton)
        
        let titleLabel = UILabel()
        titleLabel.setupLabel(ofSize: 24, weight: .semibold, txtColor: UIColor("#141414"))
        titleLabel.text = "肺气"
        navBar.addSubview(titleLabel)
        
        view.addSubview(siteListView)
        view.addSubview(cityView)
        
        
        
        navBar.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(UIDevice.isX() ? 30 : 0)
            make.left.right.equalToSuperview()
            make.height.equalTo(65)
        }
        menuButton.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(15)
            make.height.width.equalTo(20)
        }
        searchButton.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-15)
            make.height.width.equalTo(20)
        }
        titleLabel.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        
        
        
        cityView.snp.makeConstraints { (make) in
            make.top.equalTo(navBar.snp.bottom)
            make.left.right.equalToSuperview()
            
        }
        siteListView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.top.equalTo(cityView.snp.bottom)
        }
    }
    
    func loadData() {
//        let plistData = BAPlistData.shared
//        plistData.saveData(data: ["city":"tttttt" , "sfsdf":"sgsg"])
        let networkingManager = TNetworkingManager.shared
        // 空气数据请求
        networkingManager.requestData(api: .APICityAir) { (JSON) in
            let showapi_res_body = JSON["showapi_res_body"]
            let ret_code = showapi_res_body["ret_code"].int
            guard ret_code == 0 else {
                return
            }
            
            let pmJSON = showapi_res_body["pm"].dictionaryObject
            let pmModel = Mapper<BAAQICityModel>().map(JSON: pmJSON!)
            self.cityView.cityModel = pmModel
            
            
            let siteListJSON = showapi_res_body["siteList"].arrayObject
            let siteListModels = Mapper<BAAQICityModel>().mapArray(JSONArray: siteListJSON as! [[String : Any]])
            self.siteListView.siteListModels = siteListModels
            
        }
        
        networkingManager.requestData(api: .APIWeather) { (JSON) in
            
            let json = JSON["HeWeather6"][0]
            let statue = json["status"].stringValue
            if (statue != "ok") {
                printDebug("返回数据错误:\n\(json)")
                return
            }
            let weatherJSON = json["now"].dictionaryObject
            let weatherModel = Mapper<BANowModel>().map(JSON: weatherJSON ?? ["":""])
            self.cityView.weatherModel = weatherModel
        }
    }
    
    @objc func openMenuView() {
        printDebug("openMenuView")
    }
    @objc func openSearchView() {
        printDebug("openSearchView")
        let searchView = BASearchViewController()
        present(searchView, animated: false, completion: nil)
        searchView.searchBack = { (city) in
            let networkingManager = TNetworkingManager.shared
            networkingManager.city = city
            self.loadData()
        }
    }
    @objc func openListView() {
        printDebug("openListView")
    }
    
    
    
    
}





